﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Routing;

namespace NmUI
{
    public static class NmActionLink
    {
        public static MvcHtmlString ToolBarActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions)
        {
            var iscTag = new TagBuilder("span");
            iscTag.AddCssClass("is_c");
            var imgTag = new TagBuilder("img");
            imgTag.AddCssClass(actionName.ToLower() + "_img");
            imgTag.MergeAttribute("src", "/Content/themes/admin/images/invis.gif");
            iscTag.InnerHtml += imgTag;
            var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, null, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);
            var spanLink = new TagBuilder("span") { InnerHtml = linkText };
            var tag = new TagBuilder("a");            
            tag.InnerHtml += iscTag;
            tag.InnerHtml += spanLink;
            tag.MergeAttribute("href", targetUrl);
            tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }
        public static MvcHtmlString ToolBarActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions, object htmlAttributes)
        {
            var iscTag = new TagBuilder("span");
            iscTag.AddCssClass("is_c");
            var imgTag = new TagBuilder("img");
            imgTag.AddCssClass(actionName.ToLower() + "_img");
            imgTag.MergeAttribute("src", "/Content/themes/admin/images/invis.gif");
            iscTag.InnerHtml += imgTag;
            var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, null, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);
            var spanLink = new TagBuilder("span") { InnerHtml = linkText };
            var tag = new TagBuilder("a");            
            tag.InnerHtml += iscTag;
            tag.InnerHtml += spanLink;
            tag.MergeAttribute("href", targetUrl);
            tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }
        public static MvcHtmlString ToolBarActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
        {
            var iscTag = new TagBuilder("span");
            iscTag.AddCssClass("is_c");
            var imgTag = new TagBuilder("img");
            imgTag.AddCssClass(actionName.ToLower() + "_img");
            imgTag.MergeAttribute("src", "/Content/themes/admin/images/invis.gif");
            iscTag.InnerHtml += imgTag;
            var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, null, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);
            var spanLink = new TagBuilder("span") { InnerHtml = linkText };
            var tag = new TagBuilder("a");            
            tag.InnerHtml += iscTag;
            tag.InnerHtml += spanLink;
            tag.MergeAttribute("href", targetUrl);
            tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }
        public static MvcHtmlString ToolBarActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
        {
            var iscTag = new TagBuilder("span");
            iscTag.AddCssClass("is_c");
            var imgTag = new TagBuilder("img");
            imgTag.AddCssClass(actionName.ToLower() +"_img");
            imgTag.MergeAttribute("src", "/Content/themes/admin/images/invis.gif");
            iscTag.InnerHtml += imgTag;
            var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);
            var spanLink = new TagBuilder("span"){InnerHtml = linkText};
            var tag = new TagBuilder("a");            
            tag.InnerHtml += iscTag;
            tag.InnerHtml += spanLink;
            tag.MergeAttribute("href", targetUrl);
            tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }        
    }
}
